package sk.marek.barak.client;

import com.google.gwt.user.client.ui.Composite;


import com.google.gwt.user.client.ui.PushButton;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.ui.Label;

/**
 * 
 * @author user
 *
 */

public class UserChat extends Composite {
	private UserTab user;
	private TextArea chatArea;
	private TextBox messageBox;
	private Label lblName;
	private PushButton minimizeButton;
	/**
	 * 
	 * @param table = defines the widgets parent, it actually hold the widged
	 * @param userName = defines the user to whom is the chat opened
	 */
	
	public UserChat(final UserTab user) {
		this.user = user;
		/**
		 * 
		 */
		
		chatArea = new TextArea();
		messageBox = new TextBox();
		lblName = new Label(user.getUserName());
		
		AbsolutePanel absolutePanel = new AbsolutePanel();
		initWidget(absolutePanel);
		absolutePanel.setSize("219px", "295px");
		
		
		chatArea.setStyleName("gwt-TextArea2");
		chatArea.setReadOnly(true);
		absolutePanel.add(chatArea, 0, 22);
		chatArea.setSize("212px", "233px");
		
		
		absolutePanel.add(messageBox, 0, 261);
		messageBox.setSize("152px", "21px");
		messageBox.addKeyPressHandler(new KeyPressHandler() {
			
			@Override
			public void onKeyPress(KeyPressEvent event) {
				if(event.getCharCode() == KeyCodes.KEY_ENTER)
					sendMessage();
				
			}
		});
		
		PushButton closeButton = new PushButton("x");
		absolutePanel.add(closeButton, 180, 0);
		closeButton.setSize("24px", "15px");
		closeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event){
				closeWindow();
				
			}
		});
		
		
		PushButton sendButton = new PushButton("send");
		absolutePanel.add(sendButton, 160, 261);
		sendButton.setSize("44px", "25px");
		sendButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				sendMessage();
				
			}
		});
	
		
		
		absolutePanel.add(lblName, 0, 0);
		lblName.setSize("80px", "21px");
		
		minimizeButton = new PushButton("_");
		minimizeButton.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				user.closeChat();
				
			}
		});
		absolutePanel.add(minimizeButton, 145, 0);
		minimizeButton.setSize("24px", "15px");
		
	}
	private void sendMessage(){
		if(!"".equals(messageBox.getText())){
			user.sendMessage(messageBox.getText());
			String text = "Me: "+ messageBox.getText();
			chatArea.setText(chatArea.getText()+text+'\n');
			messageBox.setText("");
			}
		
	}
	public void sendMessage(String msg){
		if(!"".equals(msg)){
			chatArea.setText(chatArea.getText()+msg+'\n');
			
		}
	}
	public void closeWindow(){
		user.closeChatTab();
		
	}
	
}
